﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace DamageCalculator.SubForms
{
    public partial class GamesSettings : Form
    {
        List<DamageCalculator.Classes.GameSettings> GS_List;

        public delegate void NothingPassedDelegate();
        public bool changesSaved = false;

        public event NothingPassedDelegate SettingsUpdateRequired;

        public GamesSettings()
        {
            InitializeComponent();

            this.GS_List = new List<Classes.GameSettings>();
            String MydocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Damage Calculator";
            if (!Directory.Exists(MydocsPath + @"\Game Settings Files"))
                Directory.CreateDirectory(MydocsPath + @"\Game Settings Files");

            String[] Files = Directory.GetFiles(MydocsPath + @"\Game Settings Files");

            if (Files.Length == 0)
            {
                this.GS_List.Add(new Classes.GameSettings());
                this.PropGrid.SelectedObject = this.GS_List[0];
            }
            else
            {
                bool isfirst = true;
                foreach (String F in Files)
                {
                    this.GS_List.Add(DamageCalculator.Classes.StaticFunctions.ReadXML(F));
                    ToolStripMenuItem T = new ToolStripMenuItem(Path.GetFileNameWithoutExtension(F));
                    T.Click += new EventHandler(GameSelector_Click);
                    T.CheckOnClick = true;
                    if (isfirst)
                    {
                        T.Checked = true;
                        isfirst = false;
                    }
                    this.selectGameToolStripMenuItem.DropDownItems.Add(T);
                }

                this.GameSelector_Click(this.selectGameToolStripMenuItem.DropDownItems[0], null);
            }
        }

        private void GameSelector_Click(object sender, EventArgs E)
        {

            ToolStripMenuItem T =  sender as ToolStripMenuItem;
            foreach (ToolStripItem Ts in this.selectGameToolStripMenuItem.DropDownItems)
            {
                ToolStripMenuItem tm = Ts as ToolStripMenuItem;
                if (tm.Checked && tm != T)
                {
                    tm.Checked = false;
                    break;
                }
            }
            this.PropGrid.SelectedObject = this.GS_List.Find(
                delegate(Classes.GameSettings G)
                {
                    return G.GameName == T.Text;
                }
            );
        }

        private void ReloadGames()
        {
            this.selectGameToolStripMenuItem.DropDownItems.Clear();
            String MydocsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Damage Calculator";
            String[] Files = Directory.GetFiles(MydocsPath + @"\Game Settings Files");

             if (Files.Length == 0)
             {
                 this.GS_List.Add(new Classes.GameSettings());
                 this.PropGrid.SelectedObject = this.GS_List[0];
             }
             else
             {
                 foreach (String F in Files)
                 {
                     this.GS_List.Add(DamageCalculator.Classes.StaticFunctions.ReadXML(F));
                     ToolStripMenuItem T = new ToolStripMenuItem(Path.GetFileNameWithoutExtension(F));
                     T.Click += new EventHandler(GameSelector_Click);
                     T.CheckOnClick = true;
                     if (F == Files[Files.Length - 1])
                     {
                         T.Checked = true;
                     }
                     this.selectGameToolStripMenuItem.DropDownItems.Add(T);
                 }

                 this.GameSelector_Click(this.selectGameToolStripMenuItem.DropDownItems[this.selectGameToolStripMenuItem.DropDownItems.Count - 1], null);
             }

        }


        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.changesSaved = true;
            Classes.StaticFunctions.WriteXML(this.PropGrid.SelectedObject as Classes.GameSettings);
            this.ReloadGames();
        }

        private void selectGameToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (ToolStripItem T in this.selectGameToolStripMenuItem.DropDownItems)
            {
                ToolStripMenuItem temp = T as ToolStripMenuItem;
                temp.Checked = false;
            }
            this.PropGrid.SelectedObject = new Classes.GameSettings();
        }

        private void GamesSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.changesSaved && this.SettingsUpdateRequired != null)
                this.SettingsUpdateRequired();
        }
    }
}
